/*
 * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package top.continew.admin.cakerun.service.impl;

import lombok.RequiredArgsConstructor;

import org.springframework.stereotype.Service;

import top.continew.admin.cakerun.model.entity.BrandAccountDO;
import top.continew.admin.cakerun.service.BrandAccountService;
import top.continew.admin.common.base.service.BaseServiceImpl;
import top.continew.starter.extension.crud.model.entity.BaseIdDO;
import top.continew.starter.extension.crud.model.query.PageQuery;
import top.continew.starter.extension.crud.model.resp.PageResp;

import top.continew.admin.cakerun.mapper.FeedbackInfoMapper;
import top.continew.admin.cakerun.model.entity.FeedbackInfoDO;
import top.continew.admin.cakerun.model.query.FeedbackInfoQuery;
import top.continew.admin.cakerun.model.req.FeedbackInfoReq;
import top.continew.admin.cakerun.model.resp.FeedbackInfoDetailResp;
import top.continew.admin.cakerun.model.resp.FeedbackInfoResp;
import top.continew.admin.cakerun.service.FeedbackInfoService;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 评论反馈业务实现
 *
 * @author tangyu
 * @since 2025/07/08 10:47
 */
@Service
@RequiredArgsConstructor
public class FeedbackInfoServiceImpl extends BaseServiceImpl<FeedbackInfoMapper, FeedbackInfoDO, FeedbackInfoResp, FeedbackInfoDetailResp, FeedbackInfoQuery, FeedbackInfoReq> implements FeedbackInfoService {
    private final BrandAccountService brandAccountService;

    @Override
    public PageResp<FeedbackInfoResp> page(FeedbackInfoQuery query, PageQuery pageQuery) {
        PageResp<FeedbackInfoResp> page = super.page(query, pageQuery);
        List<FeedbackInfoResp> list = page.getList();
        List<Integer> accountIds = list.stream().map(FeedbackInfoResp::getAccountId).distinct().toList();
        List<BrandAccountDO> brandAccountDOS = this.brandAccountService.findByIds(accountIds);
        Map<Long, String> accountMap = brandAccountDOS.stream()
            .collect(Collectors.toMap(BaseIdDO::getId, BrandAccountDO::getBrand));
        list.forEach(feedbackInfoResp -> {
            String accountName = accountMap.get(Long.valueOf(feedbackInfoResp.getAccountId()));
            feedbackInfoResp.setAccountName(accountName);
        });
        return page;
    }
}